//Demo
layui.config({
    base: '/lib/winui/' //指定 winui 路径
    , version: '1.0.0-beta'
}).extend({
    window: 'js/winui.window'
}).define(['table', 'jquery', 'winui', 'window', 'layer', 'form', 'layedit', 'laydate'], function (exports) {
    winui.renderColor();
    var form = layui.form
        , layer = layui.layer
        , layedit = layui.layedit
        , laydate = layui.laydate;
    var $ = layui.jquery;
    $.get("/getAccount",{},function(result) {
        $("#creatorId").val(result.accId);
    });

    laydate.render({
        elem: '#createTime'
    });

    laydate.render({
        elem: '#updateTime'
    });

    form.verify({
        categoryId: function (value) {
            if (value.length > 64) {
                return '长度不能大于64';
            }
            if (value.length == "") {
                return '必填项不能为空';
            }
        },
        categoryName: function (value) {
            if (value.length > 128) {
                return '长度不能大于128';
            }
            if (value.length == "") {
                return '必填项不能为空';
            }
        },
        creatorId: function (value) {
            if (value.length > 128) {
                return '长度不能大于128';
            }
        },
        categoryPid: function (value) {
            if (value.length > 64) {
                return '长度不能大于64';
            }
        },
        createTime: function (value) {
            if (value.length > 7) {
                return '长度不能大于7';
            }
            if (value.length == "") {
                return '必填项不能为空';
            }
        },
        updateTime: function (value) {
            if (value.length > 7) {
                return '长度不能大于7';
            }
            if (value.length == "") {
                return '必填项不能为空';
            }
        },

    });

    //监听提交
    form.on('submit(formDemo)', function (data) {
        $ = layui.jquery;
        layer = layui.layer;
        var waitIndex = '';
        $.ajax({
            url: "/newsCategoryController/save",
            type: "POST",
            data: data.field,
            dataType: 'json',
            beforeSend: function () {
                waitIndex = layer.load();//删除前打开遮挡层
            },
            success: function (returnData) {
                if (returnData.code == 500) {
                    layer.msg(returnData.message, {icon: 2, time: 1500, shade: 0.1}, function () {
                    });

                } else {
                    layer.msg(returnData.message, {icon: 1, time: 1500, shade: 0.1}, function () {
                        top.winui.window.close('add');
                    });

                }


            }, complete: function () {
                layer.close(waitIndex);
            }
        });
        return false;
    });
});